package cn.kgc.vueebook.controller;


import cn.kgc.vueebook.entity.ResultEntity;
import cn.kgc.vueebook.pojo.Category;
import cn.kgc.vueebook.pojo.Entry;
import cn.kgc.vueebook.service.EntryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EntryController {
    @Resource
    EntryService enrtyService;

    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Entry>> selectEntry(@RequestParam(value = "categoryId",required = false,defaultValue = "0") Integer categoryId,
                                                     @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize){
        PageInfo<Entry> pageInfo=enrtyService.select(pageNum,pageSize,categoryId);
        return ResultEntity.successWithoutData(pageInfo);
    }
    //前端请求post,参数是json必须添加@RequestBody
    @RequestMapping("/saveEntry")
    public ResultEntity<String> saveEntry(@RequestBody Entry entry){
        try{
            Integer integer = 0;
            if (entry.getId() == null) {
                //添加
                integer = enrtyService.add(entry);
            } else {
                integer = enrtyService.update(entry);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch(Exception ex) {
           return ResultEntity.failed(ex.getMessage());
        }
    }
    //根据id查图书
    @RequestMapping("/getEntryById")
    public ResultEntity<Entry> getEntryId(Integer id){
        Entry ebookEntry=enrtyService.selectById(id);
        return  ResultEntity.successWithoutData(ebookEntry);
    }
    //根据id删除
    @RequestMapping("/delEntryId")
    public ResultEntity<String> delEntryId(Integer id) {
        try{
            Integer integer = enrtyService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }

    }
}
